package org.jeecg.modules.util;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONArray;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;

public class WeChatUtil {
    public static void main(String[] args){
        String appId ="your_appid";

        String appSecret ="your_appsecret";
        int count =5;

       /* //获取access_token
         String accessToken = getAccessToken(appId, appSecret);
        // 获取最新文章列表
         List<Article> articleList = getArticleList(accessToken, count);
         for (Article article : articleList){
             System.out.println(article.getTitle()+":"+ article.getUrl());
             //获取单篇文章内容
             ArticleContent articleContent = getArticleContent(accessToken, article.getMediaId());
             System.out.println(articleContent.getTitle()+":"+ articleContent.getContent());
         }*/
    }



    /*public static String getAccessToken(String appId, String appSecret){
        String url =";appid="+ appId +"&secret="+ appSecret;
        String result = HttpUtil.get(url);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject.getStr("access_token");
    }

    public static List<Article> getArticleList(String accessToken, int count){
        String url =";"+ accessToken;
        JSONObject params = new JSONObject();
        params.put("type","news");
        params.put("offset",0);
        params.put("count", count);
        String result = HttpUtil.post(url, params.toString());
        JSONObject jsonObject = JSONUtil.parseObj(result);
        JSONArray jsonArray = jsonObject.getJSONArray("item");
        List<Article> articleList = new ArrayList<>();
        for (int i =0; i < jsonArray.size();i++){
            JSONObject item = jsonArray.getJSONObject(i);
            JSONArray newsItem = item.getJSONArray("content").getJSONObject("news_item");
            JSONObject firstItem = newsItem.getJSONObject(0);
            String title = firstItem.getStr("title");
            String url = firstItem.getStr("url");
            String mediaId = item.getStr("media_id");
            articleList.add(new Article(title, url, mediaId));
        }
        return articleList;
    }


    public static ArticleContent getArticleContent(String accessToken, String mediaId){
        String url =";"+ accessToken;
        JSONObject params = new JSONObject();
        params.put("media_id", mediaId);
        String result = HttpUtil.post(url, params.toString());
        JSONObject jsonObject = JSONUtil.parseObj(result);
        JSONArray newsItem = jsonObject.getJSONArray("news_item");
        JSONObject firstItem = newsItem.getJSONObject(0);
        String title = firstItem.getStr("title");
        String content = firstItem.getStr("content");
        return new ArticleContent(title, content);
    }*/

    static class Article {
        private String title;
        private String url;
        private String mediaId;
        public Article(String title, String url){
            this.title = title;
            this.url = url;
        }

        public Article(String title, String url, String mediaId){
            this.title = title;
            this.url = url;
            this.mediaId = mediaId;
        }

        public String getTitle(){
            return title;
        }

        public void setTitle(String title){
            this.title = title;
        }

        public String getUrl(){
            return url;
        }

        public void setUrl(String url){
            this.url = url;
        }


        public String getMediaId(){
            return mediaId;
        }

        public void setMediaId(String mediaId){
            this.mediaId = mediaId;
        }
    }

    static class ArticleContent {
        private String title;
        private String content;
        public ArticleContent(String title, String content){
            this.title = title;
            this.content = content;
        }

        public String getTitle(){
            return title;
        }

        public void setTitle(String title){
            this.title = title;
        }

        public String getContent(){
            return content;
        }

        public void setContent(String content){
            this.content = content;
        }
    }
}